<?

interface Converter{
    public function convert(SMMessagePackage $package);
}

class SerializePackageConverter implements Converter{
    public static $instance=null;
    
    public function __construct(){}

    public static function getInstance(){
        if(self::$instance === null){
            self::$instance = new SerializePackageConverter();
        }

        return self::$instance;
    }

    public function convert(SMMessagePackage $package){
        // this function will convert the SMMessagePackage object into a serialized array
        // the resulting array is ready for consumption by the receiver app

        $content = array();
        $content['transmitter_base_url'] = $package->getTransmitterBaseUrl();
        $content['application_key'] = $package->getApplicationKey();
        $content['messages'] = $this->convertSMMessages($package->getSMMessages());

        return serialize($content);
    }

    public function convertSMMessages(array $sm_messages){
        $content = array();

        foreach($sm_messages as $sm_message){
            $content[] = array("message" => $sm_message->getMessage(),
                               "message_type" => $sm_message->getMessageType(),
                               "message_line" => $sm_message->getMessageLine(),
                               "message_file" => $sm_message->getMessageFile(),
                               "php_self" => $sm_message->getPHPSelf());
        }

        return $content;
    }
}
?>
